﻿using System.Collections.Generic;
using System.Windows;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        /// <summary>
        /// 令叶面板向右移动一层或由叶首面板左侧移动到右侧（位置由其索引值决定）。
        /// 几种情况：
        /// ①叶首面板，总是在中央，不能移动。
        /// ②是叶首面板的直接下级，且方向在右，不能移动——因为不能变成自身的下级。
        /// ③是叶首面板的直接下级，且方向在左，改其方向为右。
        /// ④不是叶首面板的直接下级，且方向向右，不能移动——因为不能变成自身的下级。
        /// ⑤不是叶首面板的直接下级，且方向向左，移动到上级的末尾，方向不变。
        /// </summary>
        public static void MoveLeafRight()
        {
            if (Globals.selectedEditor == null) return;
            if (Globals.selectedEditor.SelectedExpression == null) return;

            LunarPresentation.Expressions.Expression curEx
                = Globals.selectedEditor.SelectedExpression;

            if (curEx is Expressions.LeafPanel == false)
            {
                MessageBox.Show("请先选中一个叶面板！", Globals.appName,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            Expressions.LeafPanel curLeaf = curEx as Expressions.LeafPanel;
            if (curLeaf.LeafType == Expressions.LeafPanel.LeafPanelType.Root)
            {
                MessageBox.Show("树的叶首面板不支持平移操作！", Globals.appName,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (curLeaf.ParentExpression == null ||
                curLeaf.ParentExpression is Expressions.Tree == false) return;

            Expressions.Tree pTree = curLeaf.ParentExpression as Expressions.Tree;

            Expressions.LeafPanel preLevelLeaf = curLeaf.PreLevelLeaf;
            if (preLevelLeaf == null) return;

            List<Expressions.LeafPanel> sameLevelLeaves = preLevelLeaf.NextLevelLeaves;
            if (sameLevelLeaves == null || sameLevelLeaves.Count <= 0 ||
                sameLevelLeaves.Contains(curLeaf) == false) return;

            if (preLevelLeaf.LeafType == Expressions.LeafPanel.LeafPanelType.Root)
            {
                //看方向。如果不一致，递归改变此叶及其所有下级的方向。
                if (curLeaf.LeafType == Expressions.LeafPanel.LeafPanelType.Left)
                {
                    //仅改成右向即可。
                    ModifingItem mi = new ModifingItem() { OldSelectedExID = curLeaf.ExID };
                    
                    //首部加这个Action是为了撤销后刷新布局。因为撤销是逆序执行的。
                    mi.Add(new Action(pTree.ExID, -1, null, null, null, null,
                        ActionType.MoveLeafHorizontal, null));

                    RefreshAllNextLevelLeavesType(curLeaf, mi, Expressions.LeafPanel.LeafPanelType.Right);

                    //尾部加这个Action是为了重做后刷新布局。——重做是顺序执行。所以需要两个。
                    mi.Add(new Action(pTree.ExID, -1, null, null, null, null,
                        ActionType.MoveLeafHorizontal, null));

                    curLeaf.Editor.Do(mi);
                    curLeaf.Editor.IsModified = true;

                    pTree.RefreshLayout(true);
                }

                return;
            }
            else if (curLeaf.LeafType == Expressions.LeafPanel.LeafPanelType.Left)
            {
                //上升一层。

                ModifingItem mi = new ModifingItem() { OldSelectedExID = curLeaf.ExID };

                Action actIndex = new Action(curLeaf.ExID, -1, curLeaf.GetType().Name,
                    "Index", curLeaf.Index.ToString(),
                    preLevelLeaf.PreLevelLeaf.NextLevelLeaves.Count.ToString());

                curLeaf.Index = preLevelLeaf.PreLevelLeaf.NextLevelLeaves.Count;

                Action actPreID = new Action(curLeaf.ExID, -1, curLeaf.GetType().Name,
                    "PreLevelLeafExID", curLeaf.PreLevelLeafExID,
                    preLevelLeaf.PreLevelLeafExID);
                curLeaf.PreLevelLeafExID = preLevelLeaf.PreLevelLeafExID;

                mi.Add(actIndex); mi.Add(actPreID);

                curLeaf.Editor.Do(mi);
                curLeaf.Editor.IsModified = true;

                pTree.RefreshLayout(true);
            }
        }
    }
}
